
class EntryParserTests extends GroovyTestCase {
    void testParseExteen() {
        
        def html = parseFromFile('test/unit/exteen_entry')
        def type = getGenerator(html)
        assertEquals 'exteen.com' , type

        def content = html.breadthFirst().grep { it.'@id' == 'content' }[0]
        def title = content.depthFirst().grep { it.name() == 'A' }[0]
        assertEquals 'http://wonam.exteen.com/20080524/django-rails', title.'@href'
        assertEquals 'django จาก rails', HtmlUtils.extractText(title)

        def body = content.breadthFirst().grep { it.'@class' == 'post' }[0]       
        def bodyText = HtmlUtils.extractText(body)
        
        assertEquals 1955, bodyText.length()

        def tagNodes = content.breadthFirst().grep { it.'@class' == 'tag' }[0]
        def tags = HtmlUtils.extractText(tagNodes).split(',')
        tags[0] = tags[0].replaceAll("^Tags:", '')
        tags =  tags.collect { it.trim() }

        assertEquals (['development', 'django', 'framework'], tags)
    }

    void testParseBlogspot() {
        
        def html = parseFromFile('test/unit/blogspot_entry')
        def type = getGenerator(html)
        assertEquals 'blogger' , type
        
        def content = html.breadthFirst().grep { it.'@class' =~ /^blog-posts/ }[0]
        def title = content.depthFirst().grep { it.'@class' =~ /^post-title/ }[0].A[0]
        
        assertEquals 'http://pphetra.blogspot.com/2008/02/git-revert.html', title.'@href'
        assertEquals 'Git กับการ revert', HtmlUtils.extractText(title)

        def body = content.breadthFirst().grep { it.'@class' =~ /post-body/ }[0]
        def bodyText = HtmlUtils.extractText(body)

        assertEquals 1508, bodyText.length()

        def tagNodes = content.breadthFirst().grep { it.'@rel' == 'tag' }
        assertEquals 2, tagNodes.size()

        def tags = tagNodes.collect { it.value().text() }
        assertEquals (['git', 'vcs'], tags)
    }

    void testParseWordpress() {
        def html = parseFromFile('test/unit/wordpress_entry')
        def type = getGenerator(html)
        assertEquals 'WordPress.com', type
        
        def content = html.breadthFirst().grep { it.'@class' =~ /post/ && it.'@id' =~ /^post/ }[0]
        def bodyText = HtmlUtils.extractText(content)

        def titleNode = content.depthFirst().grep {it.name() =~ '^H'}[0]
        def title = HtmlUtils.extractText(titleNode)
        assertEquals 'วิมเบิลดั้นนัดชิง', title

        def tagNodes = html.BODY[0].breadthFirst().grep { it.'@href' =~ /http:\/\/wordpress.com\/tag/ }
        assertEquals 2, tagNodes.size()

        def tags = tagNodes.collect { it.value().text() }        
        assertEquals (['กีฬา', 'ทั่วไป'], tags)
    }

    def getGenerator(html) {
        html.depthFirst().grep { it.'@name' == 'generator' && it.name() == 'META'}[0].'@content'
    }

    def parseFromFile(file) {
        def txt =  new java.io.File(file).text
        def parser = new org.cyberneko.html.parsers.SAXParser()
        parser.setFeature("http://xml.org/sax/features/namespaces", false)
        def html = new XmlParser(parser).parseText(txt)         
        html
    }
}
